﻿<template>
  <a-modal
    title="新增静默时段"
    :width="900"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item label="时段描述" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入时段描述" v-decorator="['timespanName']" />
        </a-form-item>
        <a-form-item label="开始时间" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-time-picker
            :default-value="moment('00:00', 'HH:mm')"
            format="HH:mm"
            placeholder=""
            v-decorator="['begintTime']"
            @change="onChangebegintTime"
          />
        </a-form-item>
        <a-form-item label="结束时间" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-time-picker
            :default-value="moment('00:00', 'HH:mm')"
            format="HH:mm"
            placeholder=""
            v-decorator="['endTime']"
            @change="onChangeendTime"
          />
        </a-form-item>
        <a-form-item label="启用" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-switch v-decorator="['isEnabled', { valuePropName: 'checked' }]" />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import moment from 'moment'
import { TimeSpanSettingAdd } from '@/api/modular/main/TimeSpanSettingManage'

export default {
  data() {
    return {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 15 },
      },
      begintTimeDateString: '',
      endTimeDateString: '',
      visible: false,
      confirmLoading: false,
      form: this.$form.createForm(this),
    }
  },
  methods: {
    moment,
    // 初始化方法
    add(record) {
      this.visible = true
    },
    /**
     * 提交表单
     */
    handleSubmit() {
      const {
        form: { validateFields },
      } = this
      this.confirmLoading = true
      validateFields((errors, values) => {
        if (!errors) {
          for (const key in values) {
            // WARN: 有可能导致类型错误，注意
            if (values[key] !== null && typeof values[key] === 'object') {
              values[key] = JSON.stringify(values[key])
            }
          }
          values.begintTime = this.begintTimeDateString
          values.endTime = this.endTimeDateString
          TimeSpanSettingAdd(values)
            .then((res) => {
              if (res.success) {
                this.$message.success('新增成功')
                this.confirmLoading = false
                this.$emit('ok', values)
                this.handleCancel()
              } else {
                this.$message.error('新增失败：' + JSON.stringify(res.message))
              }
            })
            .finally((res) => {
              this.confirmLoading = false
            })
        } else {
          this.confirmLoading = false
        }
      })
    },
    onChangebegintTime(time, timeString) {
      this.begintTimeDateString = timeString
    },
    onChangeendTime(time, timeString) {
      this.endTimeDateString = timeString
    },
    handleCancel() {
      this.form.resetFields()
      this.visible = false
    },
  },
}
</script>
